package com.renwei.yunlong.activity.me;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.SendWorkActivity;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.fragment.BaseHistoryFragment;
import com.renwei.yunlong.fragment.ObjectAnnexFragment;
import com.renwei.yunlong.fragment.ObjectAttributesFragment;
import com.renwei.yunlong.fragment.ObjectRelatedFragment;
import com.renwei.yunlong.fragment.ObjectServiceFragment;
import com.renwei.yunlong.fragment.ObjectTypesFragment;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 对象详情--页面
 * <p>
 * Created by Administrator on 2017/8/29.
 */

public class ObjectInformationtActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;
    @Bind(R.id.v_pager)
    ViewPager vPager;
    private List<Fragment> list;
    private String[] titles;

    private NewDetailsAdapter adapter;
    private String from;
    private String outSourceFlag;
    private String assetId;
    private String hardwareName;
    private ObjectAttributesFragment objectAttributesFragment;


    public static void OpenAcitivity(Context context, String from, String outsourceflag, String assetId, String hardwareName) {
        Intent intent = new Intent(context, ObjectInformationtActivity.class);
        if (context instanceof Application) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra("from", from);
        intent.putExtra("outsourceflag", outsourceflag);
        intent.putExtra("assetId", assetId);
        ///这个地方跳转过来带入的assetId 为空
        intent.putExtra("hardwareName", hardwareName);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_details);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        from = getIntent().getStringExtra("from");
        assetId = getIntent().getStringExtra("assetId");
        hardwareName = getIntent().getStringExtra("hardwareName");
        outSourceFlag = getIntent().getStringExtra("outsourceflag") == null ?
                "0" : getIntent().getStringExtra("outsourceflag");
        initData();
    }

    private void initData() {
        simpleTileView.setTitle(from);

        //页面，数据源
        list = new ArrayList<>();

        if ("对象详情".equals(from)) {
            if ("2".equals(companyType) || "0".equals(outSourceFlag)) {
                simpleTileView.setRightText("发工单", this);
            }
            objectAttributesFragment = new ObjectAttributesFragment(assetId, outSourceFlag);
            list.add(objectAttributesFragment);
            list.add(new ObjectTypesFragment(outSourceFlag, assetId));
            list.add(new ObjectRelatedFragment(outSourceFlag, assetId));
            list.add(new ObjectAnnexFragment(outSourceFlag, assetId));
            if ("2".equals(companyType)) {
                titles = new String[]{"属性", "类型", "相关", "附件", "服务", "记录"};
                list.add(new ObjectServiceFragment(assetId));
            } else {
                titles = new String[]{"属性", "类型", "相关", "附件", "记录"};
            }
            list.add(new BaseHistoryFragment(assetId, "object", outSourceFlag));
        } else if ("备件详情".equals(from)) {
            list.add(new ObjectAttributesFragment(assetId, outSourceFlag));
            list.add(new ObjectTypesFragment(outSourceFlag, assetId));
            list.add(new ObjectAnnexFragment(outSourceFlag, assetId));
            list.add(new BaseHistoryFragment(assetId, "object", outSourceFlag));
            titles = new String[]{"属性", "类型", "附件", "记录"};
        }
        //ViewPager的适配器
        adapter = new NewDetailsAdapter(getSupportFragmentManager());
        vPager.setAdapter(adapter);
        vPager.setOffscreenPageLimit(list.size());
        //绑定
        tabLayout.setupWithViewPager(vPager);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_parent:
                if (objectAttributesFragment.serviceBean != null) {
                    Map<String, String> map = objectAttributesFragment.serviceBean.getRows();
                    SendWorkActivity.OpenActivity(this, assetId, hardwareName, map.get("serverName"),
                            map.get("ownerCode"), map.get("bindFlag"));
                }
                break;
            default:

                break;
        }
    }

    class NewDetailsAdapter extends FragmentStatePagerAdapter {

        public NewDetailsAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        //重写这个方法，将设置每个Tab的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}